<?php
declare (strict_types = 1);

namespace app\api\controller;
use app\common\model\MenuinModel;
use think\facade\Session;
use think\facade\Db;

    /**
	 * Created by Ailcc.com
     * 2021-11-14 12:00:00 沛霖主页
     * 
	 */
class Menuin extends Common
{
    public function getTree()
    {
        $tree = MenuinModel::getTree();
        return my_response(0,'',$tree);
    }

    public function getMenuList(){
		$list = MenuinModel::getList();
		return my_response(0,'',$list);
	}

	public function getAllList(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
		$pid = input('pid');
		$Menu = new MenuinModel();
		$list = $Menu->getFullList($pid);
		return json(['code'=>0,'msg'=>'操作成功','data'=>$list,'total'=>count($list)]);
	}

	/**
	 * 获取分类列表
	 * 2021-11-14 12:00:00 沛霖主页
	 */
	public function getGroupList(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
		$Category = new MenuinModel();
		$list = $Category->getFullList();
		return json(['code'=>0,'msg'=>'操作成功','data'=>$list]);
	}

	/**
	 * 添加分类
	 * 2021-11-14 12:00:00 沛霖主页
	 */
	public function add(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
		$data = $this->request->param();
		if(empty($data['id'])){
			unset($data['id']);
			$data['status'] = 1;
			MenuinModel::create($data);
		}else{
			MenuinModel::update($data);
		}
		return json(['code'=>0,'msg'=>'操作成功']);
	}
	//快捷管理
    public function editShu() {
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
        $admin_id = Session::get('admin_id');
        $info = Session::get('adminInfo');
        $data = $this->request->param('data');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'非法请求']);
        }

        $userList = Db::name('menu_in')->order('sort DESC')->select();
        if ($this->request->isPost()) {
            $id         = input('post.id');
            $value      = input('post.value');
            $status     = input('post.status');
            if($value == 'true'){
                $value = '1';
            }elseif($value == 'false'){
                $value = '0';
            }
            // 更新信息
            Db::name('menu_in')->where('id', $id)->update([$status => $value]);
            return json(['code'=>0,'msg'=>'操作成功']);
        }
        $this->getSide();
        return $this->fetch();
    }

	/**
	 * 删除分类
	 * 2021-11-14 12:00:00 沛霖主页
	 */
	public function del(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
		$ids = $this->request->param('ids');
		MenuinModel::destroy($ids);
		return json(['code'=>0,'msg'=>'删除成功']);
	}

}
